package cn.shopping.controller.article;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.shopping.pojo.Sh_article;
import cn.shopping.service.article.ArticleService;

@Controller
@RequestMapping("/article")
public class ArticleController {
	
	@Resource
	private ArticleService articleService;

	
	
	/**
	 * 文章列表
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/articlelist.html")
	public String articlelist( HttpSession session ,Model model ){
		List<Sh_article> list = articleService.getArticlelist();
		model.addAttribute("articlelist",list);
		return "wenzhangliebiao";
	}
	
	/**
	 * 文章跳转
	 * @return
	 */
	@RequestMapping(value="/gotoAddArticle.html",method=RequestMethod.GET)
	public String toAddArticle(){
		return "wenzhangtianjia";
	}
	/**
	 * 文章提交
	 * @param article
	 * @param session
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/addArticle.html",method=RequestMethod.POST)
	public String addArticle(Sh_article article,HttpSession session ,Model model,HttpServletRequest request){
		
		article.setStartday(new Date());
		if (articleService.addArticle(article)==1) {
			return "redirect:/article/articlelist.html";
		}

		return "wenzhangtianjia";
	}
	
	
	/**
	 * 删除文章
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/delArticle.html",method=RequestMethod.GET)
	public String delArticle(@RequestParam(value="id",required=false) String id,Model model){
		Sh_article article = articleService.getArticle(Integer.parseInt(id));
		
		model.addAttribute(article);
		article.setModifyday(new Date());
		if (articleService.delArticle(article)==1) {
			return "redirect:/article/articlelist.html";
		}
		return "redirect:/article/articlelist.html";
	}

	
	@RequestMapping(value="/tomodifyArticle.html",method=RequestMethod.GET)
	public String tomodifyArticle(@RequestParam(value="id",required=false) String id ,Model model){
		
		Sh_article article = articleService.getArticle(Integer.parseInt(id));
		
		model.addAttribute(article);
		return "wenzhangxiugai";
		
	}
	
	/**
	 * 修改文章
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/modifyArticle.html" ,method=RequestMethod.POST)
	public String modifyArticle(
			@RequestParam(value="id",required=false) int id,
			@RequestParam(value="articletitle",required=false) String articletitle,
			@RequestParam(value="articleContent",required=false) String articleContent,
			Sh_article article,Model model){
		article.setId(id);
		article.setArticletitle(articletitle);
		article.setArticleContent(articleContent);
		article.setModifyday(new Date());
		model.addAttribute(article);
		if (articleService.modifyArticle(article)==1) {
			return "redirect:/article/articlelist.html";
		}
		return "wenzhangxiugai";
	}

}
